"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mj_serverReqHandler = void 0;
const ServerConfig_1 = require("../src/config/ServerConfig");
const SocketConnectionManager_1 = require("../src/engine/socketserver/socket/SocketConnectionManager");
const Logger_1 = require("../src/engine/utils/Logger");
const Md5Helper_1 = require("../src/engine/utils/Md5Helper");
const WebUtil_1 = require("../src/engine/utils/WebUtil");
const SystemSettingModel_1 = require("../src/model/SystemSettingModel");
const request = require('request');
const crypto = require("crypto");
const routerMap = {
    updateServerStatus: "/updateServerStatus",
    online: "/online",
    update_system_setting: "/update_system_setting"
};
class mj_serverReqHandler {
    init(app) {
        for (const key in routerMap) {
            app.use(routerMap[key], this.onBaseRoot.bind(this));
        }
    }
    onBaseRoot(req, resp) {
        try {
            let ip = WebUtil_1.WebUtil.getIpByExpressReq(req);
            Logger_1.Logger.log("onBaseRoot==", ip, req.baseUrl);
            if (req.baseUrl == routerMap.updateServerStatus) {
                ServerConfig_1.ServerConfig.initConfig(ServerConfig_1.ServerConfig.nowServerConfig.nowPort);
                resp.end("success");
            }
            else if (req.baseUrl == routerMap.online) {
                let count = SocketConnectionManager_1.SocketConnectionManager.instance.getConnectionCount();
                let result = {
                    "count": count
                };
                resp.end(JSON.stringify(result));
            }
            else if (req.baseUrl == routerMap.update_system_setting) {
                SystemSettingModel_1.SystemSettingModel.instance.init();
                resp.end("success");
            }
            // else if (req.baseUrl == routerMap.onlie_club) {
            //     this.on_onlie_club(req, resp, ip)
            // }
            // else if (req.baseUrl == routerMap.is_in_server) {
            //     this.on_is_in_server(req, resp, ip)
            // }
            // else if (req.baseUrl == routerMap.is_user_online) {
            //     this.on_is_user_online(req, resp, ip)
            // }
        }
        catch (error) {
            Logger_1.Logger.warn("onBaseRoot error", error);
        }
    }
    getBody(ip, req, resp, callback) {
        // Logger.log("getBody===", req.body)
        // Logger.log("getBody==query=", req.query)
        if (req.body) {
            try {
                // let jsonObj = JSON.parse(req.body);
                // let data: object = jsonObj["data"]
                callback && callback(req, resp, req.body, ip);
            }
            catch (error) {
                Logger_1.Logger.warn("收到非法内部Http请求body", ip, req.body, error);
                resp.end();
            }
        }
        else {
            resp.end();
        }
    }
    checkBody(ip, req, resp, callback) {
        if (req.body) {
            try {
                let jsonObj = JSON.parse(req.body);
                let k = jsonObj["k"];
                let data = jsonObj["data"];
                if (k == Md5Helper_1.Md5Helper.getMd5_encrypt(JSON.stringify(data))) {
                    callback && callback(req, resp, data, ip);
                }
                else {
                    Logger_1.Logger.warn("收到非法Http请求 key", ip, req.body);
                    resp.end();
                }
            }
            catch (error) {
                Logger_1.Logger.warn("收到非法Http请求body", ip, req.body, error);
                resp.end();
            }
        }
        else {
            resp.end();
        }
    }
}
exports.mj_serverReqHandler = mj_serverReqHandler;
mj_serverReqHandler.instance = new mj_serverReqHandler();
